<?php

namespace app\controller\property\inspection;

use think\App;
use app\controller\property\Base;
use app\validate\inspection\InspectionLogValidate;
use app\common\repositories\inspection\InspectionLogRepository;
use app\common\repositories\inspection\InspectionSiteRepository;
use app\common\repositories\property\basic\PropertyBasicRepository;

class InspectionLog extends Base
{
    protected $repository;

    public function __construct(App $app, InspectionLogRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
        if (!$this->request->isAjax()) {
            $this->assign([
                'addAuth' => property_auth('propertyInspectionLogAdd'),
                'editAuth' => property_auth('propertyInspectionLogEdit'),
                'delAuth' => property_auth('propertyInspectionLogDel'),
                'detailAuth' => property_auth('propertyInspectionLogDetail')
            ]);
        }
    }

    protected function commonParams()
    {
        /** @var InspectionSiteRepository $siteRepository */
        $siteRepository = app()->make(InspectionSiteRepository::class);
        $siteData = $siteRepository->getCascaderData($this->request->propertyId, ['is_show' => 1]);
        $this->assign([
            'siteData' => $siteData,
        ]);
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param([
                'property_id' => '',
                'site_id' => '',
                'user_id' => '',
                'cate_name' => '',
                'keywords' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $data = $this->repository->getpropertyList($param, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        $this->commonParams();
        return $this->fetch('inspection/log/list');
    }


    public function add()
    {
        $siteId = (int)$this->request->param('site_id');
        /** @var InspectionSiteRepository $siteRepository */
        $siteRepository = app()->make(InspectionSiteRepository::class);
        $siteInfo = $siteRepository->get($siteId);
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'site_id' => '',
                'data' => [],
            ]);
            validate(InspectionLogValidate::class)->scene('batchAdd')->check($param);
            try {
                foreach ($param['data'] as $v) {
                    $data = $v;
                    $data['site_id'] = $param['site_id'];
                    if ($data['status'] == 1) {
                        $data['pictures'] = [];
                        $data['remark'] = '';
                    } else {
                        $data['pictures'] = implode(',', $data['pictures']);
                    }
                    $res = $this->repository->addInfo($this->request->propertyId, $data, $this->request->userId);
                }
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加巡检记录信息失败', $e);
                return $this->error('网络错误');
            }
        } else {

            $this->commonParams();
            return $this->fetch('inspection/log/add', [
                'siteInfo' => $siteInfo,
            ]);
        }
    }

    /**
     * 编辑
     * @return array|string|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息获取失败');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'site_name' => '',
                'attr_value' => '',
                'remark' => '',
                'is_show' => '',
            ]);
            validate(InspectionLogValidate::class)->scene('edit')->check($param);
            if (isset($param['picture']) && $param['picture'] !== '') {
                $param['pictures'] = implode(',', $param['picture']);
            }
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->success('数据无变化');
                }
            } catch (\Exception $e) {
                exception_log('编辑巡检记录信息失败', $e);
                return $this->success('修改失败');
            }
        } else {
            $this->commonParams();
            return $this->fetch('inspection/log/edit', [
                'info' => $info,
            ]);
        }
    }

    public function details()
    {
        $id = (int)$this->request->param('id');
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if ($info['id'] <= 0) {
            return $this->error('信息不存在');
        }
        $this->commonParams();
        return $this->fetch('inspection/log/detail', [
            'info' => $info,
        ]);
    }

    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除巡检记录信息成功');
            } else {
                return $this->error('删除巡检记录信息失败');
            }
        } catch (\Exception $e) {
            exception_log('删除巡检记录信息失败', $e);
            return $this->error('网络失败');
        }
    }


}